$(function () {
    layui.use(['table','form'],function () {
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem:'#LAY_table_fleet',
            url:'/pages/back/fleet/list',
            toolbar: '#toolbarDemo',//开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            cols:[[
                {field:'fleetid',title:'车队编号',sort: true, fixed: 'left'},
                {field:'name',title:'车队名称',sort: true, fixed: 'left'},
                {field:'introduction',title:'车队简介'},
                {
                    field: 'datetime',
                    title: '成立时间',
                    templet:"<div>{{layui.util.toDateString(d.datetime,'yyyy-MM-dd HH:mm:ss')}}</div>",
                    sort:true
                },
                {field:'right',title:'操作',templet:"#barOption"}
            ]],
        });
        table.on('tool(LAY_table_fleet)', function(obj){
            var data = obj.data;
            var fleetid = data.fleetid
            if(obj.event == "edit"){
                // alert(fleetid)
                layer.open({
                    type: 1
                    ,title: false //不显示标题栏
                    ,area: ['600px','500px'],
                    content: '<div class="layui-col-md10"><form class="layui-form" style="padding-top: 20px">' +
                    ' <div class="layui-form-item">' +
                    '<label class="layui-form-label">车队名称：</label>' +
                    '<div class="layui-input-block">' +
                    '<input type="text" name="text" required  lay-verify="required" placeholder="请输入新名字" autocomplete="off" class="layui-input">' +
                    '</div>' +
                    '</div>' +

                    ' <div class="layui-form-item">' +
                    '<label class="layui-form-label">车队简介：</label>' +
                    '<div class="layui-input-block">' +
                    '<textarea style="height: 200px" type="text" name="text" required  lay-verify="required" placeholder="请输入车队简介" autocomplete="off" class="layui-input"></textarea>' +
                    '</div>' +
                    '</div>' +
                    ' <div class="layui-form-item">' +
                    '<div class="layui-input-block">' +
                    '<input type="button" class="layui-btn layui-col-md-offset2"' +
                    'lay-submit lay-filter="formDemo" id="edit" value="提交">' +
                    '<button type="reset" class="layui-btn layui-btn-primary">重置</button>' +
                    '</div>' +
                    '</div>' +
                    '</form></div><div class="layui-col-md2">&nbsp;</div>'
                })
                $(function () {
                    $("#edit").on('click',function () {
                        var fleet = $('.layui-form').serializeArray()
                        var name = fleet[0].value
                        var introduction = fleet[1].value
                        $.ajax({
                            url:'/pages/back/fleet/edit?'+fleetid,
                            type: 'POST',
                            data:{"fleetid":fleetid,"name":name,"introduction":introduction},
                            success:function (data) {
                                if(data == "success"){ // 表示成功
                                    layer.msg('修改成功',{time:1000,icon:1,end:function () {
                                            // 登录成功之后跳转到后台首页
                                            window.location.href = "/pages/back/fleet/listPre";
                                        }})
                                }else if(data == "isEmpty"){ // 表示验证失败
                                    layer.msg('数据不能为空或者数据格式错误',{time:1000,icon:2})
                                }else{
                                    layer.msg('修改失败',{time:1000,icon:2})
                                }
                            }
                        })
                    })
                });
            }
        });
    })
})